home *** CD-ROM | disk | FTP | other *** search
- /* File "stddebug.c", Light Sockets - Copyright (C) Matt Slot, 1996 */
- /* Standard debugging and error tracking macros. */
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* Include Files */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <unistd.h>
-
- #if defined(PLATFORM_MAC)
- #include <LowMem.h>
- #include <MixedMode.h>
- #include <Notification.h>
- #endif
-
- #include "stddebug.h"
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- #define EOL "\r\n"
- #define MSG ((fatal) ? "Assert" : ((thrown) ? "Throw" : "Trace"))
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- void _DebugPrintErr(char *msg, long err, char *file, char *line,
- int thrown, int fatal) {
- if (!msg || !*msg) msg = MSG;
- if (!err) fprintf(stderr, "%s @ %s:%s" EOL, msg, file, line);
- else fprintf(stderr, "%s : %ld @ %s:%s" EOL, msg, err, file, line);
- if (fatal) exit((err) ? err : 1);
- }
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- void _DebugPrintLog(char *msg, long err, char *file, char *line,
- int thrown, int fatal) {
- static int inited = 0;
- char *logname = "logfile.txt";
- FILE *logfile = 0;
- char time_str[80];
- struct tm *time_tm;
- time_t time_now;
-
- if (logfile = fopen(logname, (!inited) ? "w" : "a")) {
- if (!inited) {
- time_now = time(0);
- time_tm = localtime(&time_now);
- strftime(time_str, sizeof(time_str), "%c", time_tm);
- fprintf(logfile, "Debug logfile dated: %s" EOL, time_str);
- fprintf(logfile, "Binary dated: " __DATE__ ", " __TIME__ EOL);
- fprintf(logfile, EOL);
- inited = 1;
- }
-
- if (!msg || !*msg) msg = MSG;
- if (!err) fprintf(logfile, "%s @ %s:%s" EOL, msg, file, line);
- else fprintf(logfile, "%s : %ld @ %s:%s" EOL, msg, err, file, line);
- fclose(logfile);
- }
- if (fatal) exit((err) ? err : 1);
- }
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- #if defined(PLATFORM_MAC)
- void _DebugMacsbug(char *msg, long err, char *file, char *line,
- int thrown, int fatal) {
- unsigned char debugstr[257];
-
- if (!msg || !*msg) msg = MSG;
- sprintf((char*)debugstr+1, "%s : %ld @ %s:%s%s", msg, err, file, line,
- (fatal || thrown) ? "" : ";g");
- debugstr[0] = strlen((char*)debugstr+1);
-
- DebugStr(debugstr);
- if (fatal) ExitToShell();
- }
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- static pascal void _DebugNotifyResp(NMRecPtr nmPtr) {
- Boolean *busy = (Boolean *) nmPtr->nmRefCon;
- *busy = false;
- }
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- void _DebugNotify(char *msg, long err, char *file, char *line,
- int thrown, int fatal) {
- const short kMaxNotifications = 10;
- static Boolean nmBusy[kMaxNotifications] = {0,0,0,0,0,0,0,0,0,0};
- static NMRec nmRec[kMaxNotifications];
- static Str255 nmStr[kMaxNotifications];
- #if GENERATINGCFM
- RoutineDescriptor nmRD =
- BUILD_ROUTINE_DESCRIPTOR(uppNMProcInfo, _DebugNotifyResp);
- NMUPP nmUPP = &nmRD;
- #else
- NMUPP nmUPP = _DebugNotifyResp;
- #endif
- long i, j, k;
-
- for(i=0; i<kMaxNotifications && nmBusy[i]; i++);
- if (i==kMaxNotifications) return;
-
- nmBusy[i] = true;
- nmRec[i].qType = nmType;
- nmRec[i].nmMark = 0;
- nmRec[i].nmIcon = 0;
- nmRec[i].nmSound = (Handle) -1;
- nmRec[i].nmStr = nmStr[i];
- nmRec[i].nmResp = nmUPP;
- nmRec[i].nmRefCon = (long) &(nmBusy[i]);
-
- #if GENERATINGCFM
- MakeDataExecutable(&nmRD, sizeof(nmRD));
- #endif
-
- if (!msg || !*msg) msg = MSG;
-
- #define _pstrcatcstr(cs,ps) \
- { BlockMoveData((cs), (ps)+(ps)[0]+1, strlen(cs)); (ps)[0]+=strlen(cs); }
- #define _pstrcatchar(ch,ps) { (ps)[++(ps)[0]] = (ch); }
-
- nmStr[i][0] = 0;
- _pstrcatcstr(msg, nmStr[i]);
- _pstrcatchar(' ', nmStr[i]);
- if (err) {
- _pstrcatchar('(', nmStr[i]);
- if (err < 0) { _pstrcatchar('-', nmStr[i]); err = 0 - err; }
- for(j=8, k=100000000; j>=0; j--,k/=10)
- if ((err/k)%10 || !j) _pstrcatchar('0' + (err/k)%10, nmStr[i]);
- _pstrcatchar(')', nmStr[i]);
- }
- _pstrcatcstr("\rFile \"", nmStr[i]);
- _pstrcatcstr(file, nmStr[i]);
- _pstrcatcstr("\" at line ", nmStr[i]);
- _pstrcatcstr(line, nmStr[i]);
-
- NMInstall(&(nmRec[i]));
- }
-
- #endif
-